package com.example.friend.service.exam.Impl;

import cn.hutool.core.collection.CollectionUtil;
import com.example.common.core.Utils.ThreadLocalUtil;
import com.example.common.core.constants.Constants;
import com.example.common.core.domin.TableDataInfo;
import com.example.friend.domain.exam.dto.ExamQueryDTO;
import com.example.friend.domain.exam.vo.ExamVO;
import com.example.friend.manager.ExamCacheManager;
import com.example.friend.mapper.exam.ExamMapper;
import com.example.friend.service.exam.IExamService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExamServiceImpl implements IExamService {

    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private ExamCacheManager examCacheManager;

    @Override
    public List<ExamVO> list(ExamQueryDTO examQueryDTO) {
        PageHelper.startPage(examQueryDTO.getPageNum(), examQueryDTO.getPageSize());
        return examMapper.selectExamList(examQueryDTO);
    }

    @Override
    public TableDataInfo redisList(ExamQueryDTO examQueryDTO) {
        //从redis当中获取竞赛列表数据
        //获取redis中数据长度,判断是否要去sql中拉取数据
        Long total = examCacheManager.getListSize(examQueryDTO.getType(),null);
        List<ExamVO> examVOList;
        if (total == null || total <= 0) {
            examVOList = list(examQueryDTO);
            //将数据同步到redis中
            examCacheManager.refreshCache(examQueryDTO.getType(),null);
            //从数据库中获取数据总长
            total = new PageInfo<>(examVOList).getTotal();
        } else {
            examVOList = examCacheManager.getExamVOList(examQueryDTO,null);
            //以防redis刷新
            total = examCacheManager.getListSize(examQueryDTO.getType(),null);
        }
        //时间过滤

        if(CollectionUtil.isEmpty(examVOList)){
            return TableDataInfo.empty();
        }
        //处理用户报名竞赛
        assembleExamVOList(examVOList);
        return TableDataInfo.success(examVOList,total);

    }

    //返回竞赛首个题目的id
    @Override
    public String getFirstQuestion(Long examId) {
        checkAndRefresh(examId);
        return examCacheManager.getFirstQuestion(examId).toString();
    }

    //竞赛题目的上一题
    @Override
    public String preQuestion(Long examId, Long questionId) {
        //校验
        checkAndRefresh(examId);
        return examCacheManager.preExamQuestion(examId,questionId).toString();
    }

    //竞赛题目的下一题
    @Override
    public String nextQuestion(Long examId, Long questionId) {
        checkAndRefresh(examId);
        return examCacheManager.nextExamQuestion(examId,questionId).toString();
    }

    //检查redis中,题目顺序列表
    public void checkAndRefresh(Long examId) {
        //先判断redis中是否有数据
        Long listSize = examCacheManager.getExamQuestionSize(examId);
        if (listSize == null || listSize <= 0) {
            //刷新缓存
            examCacheManager.refreshExamQuestionCache(examId);
        }
    }


    private void assembleExamVOList(List<ExamVO> examVOList) {
        //获取当前用户
        Long userId = ThreadLocalUtil.get(Constants.USER_ID,Long.class);
        //获取用户所有报名的竞赛
        List<Long> userExamIdList = examCacheManager.getAllUserExamList(userId);
        if (CollectionUtil.isEmpty(userExamIdList)) {
            return;
        }
        //遍历examVoList
        for (ExamVO examVO : examVOList) {
            if(userExamIdList.contains(examVO.getExamId())){
                examVO.setEnter(true);
            }
        }
    }
}
